package com.hongtong.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hongtong.auto.JSONAuthentication;
import com.hongtong.entity.TeTask;
import com.hongtong.entity.TeXk;
import com.hongtong.mapper.TeCurMapper;
import com.hongtong.mapper.TeUserMapper;
import com.hongtong.mapper.TeXkMapper;
import com.hongtong.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hongtong
 * @since 2022-04-20
 */
@RestController
@RequestMapping("/te-xk")
public class TeXkController extends JSONAuthentication {
    @Autowired
    TeXkMapper teXkMapper;
    @Autowired
    TeUserMapper teUserMapper;
    @Autowired
    TeCurMapper teCurMapper;
    @ApiOperation(value = "添加学生选课记录")
    @PostMapping("/add")
    public void addCar(HttpServletRequest request, HttpServletResponse response, @RequestBody TeXk teXk) throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",teXk.getUserId());
        wrapper.eq("class_id",teXk.getClassId());
        if(teXkMapper.selectOne(wrapper)!=null){
            Result error = Result.error().message("不可重复选课");
            this.WriteJSON(request, response, error);
        }else{
           if(teXkMapper.insert(teXk)>0){
               Result ok = Result.ok();
               this.WriteJSON(request, response, ok);
           }else{
               Result error = Result.error().message("选课失败");
               this.WriteJSON(request, response, error);
           }
        }
    }
    @ApiOperation(value = "删除选课根据id")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id ) throws Exception{
        int i=teXkMapper.deleteById(id);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查看全部选课情况")
    @PostMapping("/lookAllXk")
    public void lookAllXk(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current ) throws Exception{
        Page<TeXk> page=new Page<>(size,current);
        Page<TeXk> page1=teXkMapper.selectPage(page,null);
        List<TeXk> records = page1.getRecords();
        for (TeXk a :records){
        a.setTeUser(teUserMapper.selectById(a.getUserId()));
        a.setTeCur(teCurMapper.selectById(a.getClassId()));
        }
        int total=teXkMapper.selectCount(null);
        if(records.size()>0){
            Result ok = Result.ok().data("test",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查看全部选课情况根据课程id")
    @PostMapping("/lookAllXkByClassId")
    public void lookAllXkByClassId(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current ,@RequestParam("curId") int curId) throws Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("class_id",curId);
        Page<TeXk> page=new Page<>(size,current);
        Page<TeXk> page1=teXkMapper.selectPage(page,wrapper);
        List<TeXk> records = page1.getRecords();
        for (TeXk a :records){
            a.setTeUser(teUserMapper.selectById(a.getUserId()));
            a.setTeCur(teCurMapper.selectById(a.getClassId()));
        }
        int total=teXkMapper.selectCount(wrapper);
        if(records.size()>0){
            Result ok = Result.ok().data("test",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查看全部选课情况根据课程id")
    @PostMapping("/lookAllXkByUserId")
    public void lookAllXkByUserId(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current ,@RequestParam("curId") int curId) throws Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",curId);
        Page<TeXk> page=new Page<>(size,current);
        Page<TeXk> page1=teXkMapper.selectPage(page,wrapper);
        List<TeXk> records = page1.getRecords();
        for (TeXk a :records){
            a.setTeUser(teUserMapper.selectById(a.getUserId()));
            a.setTeCur(teCurMapper.selectById(a.getClassId()));
        }
        int total=teXkMapper.selectCount(wrapper);
        if(records.size()>0){
            Result ok = Result.ok().data("test",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查看全部选课情况根据选课id")
    @PostMapping("/lookAllXkByXkId")
    public void lookAllXkByXkId(HttpServletRequest request, HttpServletResponse response,@RequestParam("id") int id) throws Exception{
        TeXk teXk = teXkMapper.selectById(id);
            teXk.setTeUser(teUserMapper.selectById(teXk.getUserId()));
            teXk.setTeCur(teCurMapper.selectById(teXk.getClassId()));
        if(teXk!=null){
            Result ok = Result.ok().data("test",teXk);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

}

